// components/go-coupon/go-coupon.js
let app = getApp();
Component({

  options: {
    addGlobalClass: true,//使用app.wxss样式
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: false,
    couponList: [],
    ticketId: [],
  },

  lifetimes: {
    ready () {
//      this.getLogincoup();
    },

  },

  /**
   * 组件的方法列表
   */
  methods: {
    touchmove(){},

    //领取
    tipClick(){
      let _this = this;
      let user = app.getUser();

      app.requestUrl({
        method: 'POST',
        url: '/wxapi/recieveCoup',
        data: {
          ticketid: _this.data.ticketId,
          memberId: user.id,
        }
      }).then(res =>{
        wx.showToast({
          title: '领取成功！',
          icon: 'none'
        })
        _this.setData({ isShow: false })
      })
    },

    //优惠券列表
    getLogincoup(){
      let _this = this;
      let user = app.getUser();
      if(user.id == null) return;
      let isFirst = wx.getStorageSync('firstLogin');
      app.requestUrl({
        method: 'POST',
        url: '/wxapi/logincoup',
        data: {
          centreid: user.centreId,
          memberid: user.id,
          state: isFirst ? 0 : 1,//true新用户，false老用户
        }
      }).then(res =>{

        if(res.data.data=='') return;

        let temp = []
        res.data.data.forEach(item =>{
           temp.push(item.id)
        })
        _this.data.ticketId = _this.data.ticketId.join(',');
        _this.setData({
          couponList: res.data.data,
          ticketId: temp,
          isShow: true
        })
      })
    }
  }
})
